package com.css.apps.base.user.action;

import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.css.apps.base.common.provider.UserQueryProvider;
import com.css.apps.base.user.model.SUser;
import com.css.apps.base.user.model.SUserDevice;
import com.css.core.configuration.Environment;
import com.css.db.page.Page;
import com.css.db.query.QueryCache;
import com.css.util.Ajax;
import com.css.util.DateUtil;
import com.css.util.Messages;
import com.css.util.StringHelper;
import com.opensymphony.xwork.Action;

@SuppressWarnings("serial")
public class DirPersonalDev extends UserAction {

	private static Log log = LogFactory.getLog(DirPersonalDev.class);
	private Page page;
	private SUser sUser;

	private String deviceType;// 设备类型
	private String loginStatus;// 登录状态
	private Date beginLastLoginTime; // 起始最后登录时间
	private Date endLastLoginTime; // 结束最后登录时间

	public DirPersonalDev() {
		page = new Page();
		page.setCountField("a.uuid");
	}

	@Override
	protected String userGo() {
		try {
			sUser = UserQueryProvider.currentUser(); // 获取当前用户;
			if (sUser == null) {
				result = Ajax.JSONResult(Environment.RESULT_CODE_ERROR, Messages.getString("systemMsg.loginWebOut"));
				return Action.ERROR;
			}
			QueryCache qc = new QueryCache("select a.uuid from SUserDevice a " + getWhere() + getOrder());
			setWhere(qc);
			page = qc.page(page);
			page.setResults(QueryCache.idToObj(SUserDevice.class, page.getResults()));
			return SUCCESS;
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			setMessage(Messages.getString("systemMsg.exception"));
			return Action.ERROR;
		}
	}

	public String getWhere() {
		StringBuffer sb = new StringBuffer(" where a.userId = :userId and a.delFlag = '2' ");
		if (StringHelper.isNotEmpty(deviceType))
			sb.append(" and a.deviceType = :deviceType ");
		if (StringHelper.isNotEmpty(loginStatus))
			sb.append(" and a.loginStatus = :loginStatus ");
		if (beginLastLoginTime != null) {
			sb.append(" and a.lastLoginTime >=:beginLastLoginTime ");
		}
		if (endLastLoginTime != null) {
			sb.append(" and a.lastLoginTime <=:endLastLoginTime ");
		}
		return sb.toString();
	}

	public void setWhere(QueryCache qc) {
		qc.setParameter("userId", sUser.getUuid());
		if (StringHelper.isNotEmpty(deviceType))
			qc.setParameter("deviceType", deviceType.trim());
		if (StringHelper.isNotEmpty(loginStatus))
			qc.setParameter("loginStatus", loginStatus.trim());
		if (beginLastLoginTime != null) {
			qc.setParameter("beginLastLoginTime", beginLastLoginTime);
		}
		if (endLastLoginTime != null) {
			qc.setParameter("endLastLoginTime", DateUtil.addDate(endLastLoginTime, 1));
		}
	}

	public String getOrder() {
		return StringHelper.isNotEmpty(page.getOrderByString()) ? page.getOrderByString()
				: " order by a.lastLoginTime desc";
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public String getDeviceType() {
		return deviceType;
	}

	public void setDeviceType(String deviceType) {
		this.deviceType = deviceType;
	}

	public String getLoginStatus() {
		return loginStatus;
	}

	public void setLoginStatus(String loginStatus) {
		this.loginStatus = loginStatus;
	}

	public Date getBeginLastLoginTime() {
		return beginLastLoginTime;
	}

	public void setBeginLastLoginTime(Date beginLastLoginTime) {
		this.beginLastLoginTime = beginLastLoginTime;
	}

	public Date getEndLastLoginTime() {
		return endLastLoginTime;
	}

	public void setEndLastLoginTime(Date endLastLoginTime) {
		this.endLastLoginTime = endLastLoginTime;
	}

}
